import 'package:easy_chat_app/commons/GlobalStatus.dart';
import 'package:easy_chat_app/commons/utils/RouteUtils.dart';
import 'package:easy_chat_app/commons/utils/StringUtils.dart';
import 'package:easy_chat_app/commons/widget/UserHeadPortrait.dart';
import 'package:easy_chat_app/mobile/constants/MobileAppGlobalConstants.dart';
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:provider/provider.dart';

class MobileUserInfoPageDataChangeNotifier extends ChangeNotifier {}

class MobileUserInfoPage extends StatelessWidget {
  final Logger logger = Logger();

  MobileUserInfoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (context) {
          return MobileUserInfoPageDataChangeNotifier();
        })
      ],
      builder: (context, child) {
        var appUserDetailChangeNotifier =
            context.watch<AppUserDetailChangeNotifier>();
        var userDetail = appUserDetailChangeNotifier.getAppUserDetail();
        return Scaffold(
          appBar: AppBar(
            title: const Text("个人信息"),
            centerTitle: true,
            surfaceTintColor: MobileAppGlobalConstants.appBarSurfaceTintColor,
            backgroundColor: MobileAppGlobalConstants.appBarBackgroundColor,
          ),
          body: Container(
            color: const Color.fromRGBO(237, 237, 237, 1),
            child: SingleChildScrollView(
              child: Column(
                children: [
                  GestureDetector(
                    onTap: () async {
                      await RouteUtils.route2MobileUserHeadPortraitPage(
                          userDetail);
                    },
                    child: Container(
                      color: Colors.white,
                      width: double.infinity,
                      height: 80,
                      child: Container(
                        margin: const EdgeInsets.only(left: 16, right: 16),
                        decoration: const BoxDecoration(
                            border: Border(
                                bottom: BorderSide(
                                    color: Color.fromRGBO(237, 237, 237, 1)))),
                        child: Row(
                          children: [
                            Container(
                              height: 79,
                              alignment: Alignment.centerLeft,
                              child: const Text(
                                "头像",
                                style: TextStyle(
                                    fontSize: 18, color: Colors.black),
                              ),
                            ),
                            const Spacer(),
                            UserHeadPortrait.network(
                                size: 60,
                                userCode: userDetail.code!,
                                userHeadPortrait: userDetail.headPortrait!,
                                userHeadPortraitStoreType:
                                    userDetail.headPortraitStoreType!),
                            const SizedBox(
                              height: 60,
                              width: 10,
                            ),
                            const Icon(Icons.chevron_right_sharp)
                          ],
                        ),
                      ),
                    ),
                  ),
                  genUserInfoItem("昵称",
                      itemWidget: Container(
                        height: 54,
                        alignment: Alignment.centerLeft,
                        child: Text(
                          StringUtils.defaultIfEmpty(userDetail.nickname, ""),
                          style: const TextStyle(
                              fontSize: 18, color: Colors.black),
                        ),
                      ), onTap: () async {
                    await RouteUtils.route2MobileNicknameSettingPage();
                  }),
                  genUserInfoItem("EC号",
                      itemWidget: Container(
                        height: 54,
                        alignment: Alignment.centerLeft,
                        child: Text(
                          StringUtils.defaultIfEmpty(userDetail.code, ""),
                          style: const TextStyle(
                              fontSize: 18, color: Colors.black),
                        ),
                      )),
                  genUserInfoItem(
                    "我的二维码",
                    showBottomBorder: false,
                    itemWidget: Container(
                      height: 54,
                      alignment: Alignment.centerLeft,
                      child: const Icon(Icons.qr_code),
                    ),
                    onTap: () {
                      RouteUtils.route2MobileUserQRCodePage(userDetail);
                    },
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  Widget genUserInfoItem(String itemName,
      {bool showBottomBorder = true,
      Widget? itemWidget,
      void Function()? onTap}) {
    itemWidget ??= Container();
    return GestureDetector(
      onTap: onTap,
      child: Container(
        color: Colors.white,
        width: double.infinity,
        height: 55,
        child: Container(
          margin: const EdgeInsets.only(left: 16, right: 16),
          decoration: BoxDecoration(
              border: showBottomBorder
                  ? const Border(
                      bottom:
                          BorderSide(color: Color.fromRGBO(237, 237, 237, 1)))
                  : null),
          child: Row(
            children: [
              Container(
                height: 54,
                alignment: Alignment.centerLeft,
                child: Text(
                  itemName,
                  style: const TextStyle(fontSize: 16, color: Colors.black),
                ),
              ),
              const Spacer(),
              itemWidget,
              const SizedBox(
                height: 54,
                width: 10,
              ),
              const Icon(Icons.chevron_right_sharp)
            ],
          ),
        ),
      ),
    );
  }
}
